classdef BandStopKHN < Cell
    properties
        Wp;
        Wz;
        Qu;
        Ga;
    end
    
    methods 
        function this = BandStopKHN(poles, zeros, g)
            this.Name = 'KHN Band Stop';
            this.Poles = poles;
            this.Zeros = zeros;
            this.Gain = g;
            this.ImagePath='Stages Schematics/KHN_BS.PNG';
            wp = abs(poles(1));
            wz = [];    % Implicit, I don't need to specified them.
            [alpha ~] = cart2pol(real(poles(1)), imag(poles(1)));
            Q = abs(1./(2*sin(abs(alpha-pi/2))));
            this.calcCell(wp,wz,Q,g);
        end
        
        function calcCell(obj,wp,wz,Q,g)
            
            obj.Wp=wp;
            obj.Wz=wz;
            obj.Qu=Q;
            obj.Ga=g;
            
            caux=100e-9;
            zinp=100e3;
            zsum=1e3;
            resist(1)=1/(wp*caux);
            resist(2)=resist(1)/Q;
            resist(4)=zinp;
            resist(3)=sqrt(zinp/(caux^2*g*resist(1)));
            resist(5)=resist(1);
            resist(6)=resist(3);
            resist(7)=zsum;
            resist(8)=zsum;
            resist(9)=zsum;
            cap(1)=caux;
            cap(2)=caux;

            obj.Resists = resist;
            obj.Caps = cap;
            
            %Sensibilidad de wp
            senswp(1) = -0.5;
            senswp(2) = 0;
            senswp(3) = 0;
            senswp(4) = 0;
            senswp(5) = -0.5;
            senswp(6) = 0;
            senswp(7) = 0;
            senswp(8) = 0;
            senswp(9) = 0;
            senswp(10) = -0.5;
            senswp(11) = -0.5;
            %Sensibilidad de Q
            sensQ(1) = 1.5;
            sensQ(2) = -1;
            sensQ(3) = 0;
            sensQ(4) = 0;
            sensQ(5) = -0.5;
            sensQ(6) = 0;
            sensQ(7) = 0;
            senswp(7) = 0;
            senswp(8) = 0;
            senswp(9) = 0;
            senswp(10) = 0.5;
            senswp(11) = -0.5;
            %Sensibilidad de Wz
            senswz = [];
            %Sensibilidad GLP
            sensG(1) = 0.5;
            sensG(2) = 0;
            sensG(3) = -0.5;
            sensG(4) = 0.5;
            sensG(5) = 0.5;
            sensG(6) = -0.5;
            senswp(7) = 1;
            senswp(8) = -1;
            senswp(9) = -1;
            senswp(10) = 0;
            senswp(11) = 0;
            
            obj.Senswp = senswp;
            obj.Senswz = senswz;
            obj.SensQ = sensQ;
            obj.SensG = sensG;
            
            obj.error = ~obj.validateComponents(obj.Resists,obj.Caps);
        end
        
        function componentChange(obj,component,value)
            
            obj.error = 0;
            BackUPR=obj.Resists;
            BackUPC=obj.Caps;
            
            zinp=10e3;
            caux=100e-9;
            zsum=1e3;
            switch (component)
                case 1
                    obj.Resists(1)=value;
                    obj.Caps(1)=1/(obj.Wp*value);
                    obj.Caps(2)=obj.Caps(1);
                    obj.Resists(2)=value/obj.Qu;
                    obj.Resists(3)=sqrt(zinp/(obj.Ga*obj.Caps(1)^2)*value);
                    obj.Resists(4)=zinp;
                    obj.Resists(5)=value;
                    obj.Resists(6)=obj.Resists(3);
                case 2
                    obj.Resists(2)=value;
                    obj.Resists(1)=value*obj.Qu;
                    obj.Caps(1)=1/(obj.Wp*obj.Resists(1));
                    obj.Caps(2)=obj.Caps(1);
                    obj.Resists(3)=sqrt(zinp/(obj.Ga*obj.Caps(1)^2*obj.Resists(1)));
                    obj.Resists(4)=zinp;
                    obj.Resists(5)=obj.Resists(1);
                    obj.Resists(6)=obj.Resists(3); 
                    
                case 3  % Se modifico R3            
                    obj.Resists(3)=value;
                    obj.Resists(1)=1/(obj.Wp*caux);
                    obj.Resists(2)=obj.Resists(1)/obj.Qu;
                    obj.Resists(4)=zinp;
                    obj.Resists(5)=obj.Resists(1);
                    obj.Resists(6)=zinp/(value*obj.Ga*caux^2*obj.Resists(1));
                    obj.Caps(1)=caux;
                    obj.Caps(2)=caux;
                case 4
                    obj.Resists(4)=value;
                    obj.Resists(1)=1/(caux*obj.Wp);
                    obj.Resists(2)=obj.Resists(1)/obj.Qu;
                    obj.Caps(1)=caux;
                    obj.Caps(2)=obj.Caps(1);
                    obj.Resists(3)=sqrt(value/(obj.Ga*obj.Caps(1)^2*obj.Resists(1)));
                    obj.Resists(5)=obj.Resists(1);
                    obj.Resists(6)=obj.Resists(3);
                case 5
                    obj.Resists(5)=value;
                    obj.Resists(1)=value;
                    obj.Caps(1)=1/(obj.Wp*value);
                    obj.Caps(2)=obj.Caps(1);
                    obj.Resists(2)=value/obj.Qu;
                    obj.Resists(3)=sqrt(zinp/(obj.Ga*obj.Caps(1)^2)*value);
                    obj.Resists(4)=zinp;
                    obj.Resists(6)=obj.Resists(3);
                case 6
                    obj.Resists(6)=value;
                    obj.Resists(1)=1/(obj.Wp*caux);
                    obj.Resists(2)=obj.Resists(1)/obj.Qu;
                    obj.Resists(3)=zinp/(value*obj.Ga*caux^2*obj.Resists(1));
                    obj.Resists(4)=zinp;
                    obj.Resists(5)=obj.Resists(1);
                    obj.Caps(1)=caux;
                    obj.Caps(2)=caux;
                case 7
                    obj.Caps(1)=value;
                    obj.Resists(1)=1/(obj.Wp*value);
                    obj.Resists(2)=obj.Resists(1)/obj.Qu;
                    obj.Resists(4)=zinp;
                    obj.Resists(3)=sqrt(zinp/(value^2*obj.Ga*obj.Resists(1)));
                    obj.Resists(5)=obj.Resists(1);
                    obj.Resists(6)=obj.Resists(3);
                    obj.Caps(2)=value;
                case 8
                    obj.Caps(2)=value;
                    obj.Resists(1)=1/(obj.Wp*value);
                    obj.Resists(2)=obj.Resists(1)/obj.Qu;
                    obj.Resists(4)=zinp;
                    obj.Resists(3)=sqrt(zinp/(value^2*obj.Ga*obj.Resists(1)));
                    obj.Resists(5)=obj.Resists(1);
                    obj.Resists(6)=obj.Resists(3);
                    obj.Caps(1)=value;
            end
            obj.Resists(7)=zsum;
            obj.Resists(8)=zsum;
            obj.Resists(9)=zsum;
            
            status = obj.validateComponents(obj.Resists,obj.Caps);
            
            if (~status)
                obj.Resists=BackUPR;
                obj.Caps=BackUPC;
                obj.error = 1;
            end
        end
    end
end

